<?php

namespace App\Store\Controllers\Sysset;

use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Sysset\Score;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Validate\Sysset\Score as ValidateScore;

/**
 * 积分规则
 */
class ScoreController extends Controller
{
    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['store_ids', 'goods_category_names', 'type', 'value', 'rate', 'multiplier', 'start_time', 'end_time', 'min_discount']);
        $Validate = new ValidateScore('add');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        // 组装数据
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $store_ids = $req_data['store_ids'];
        $store_ids = is_array($store_ids) ? $store_ids : (is_array(json_decode($store_ids, true)) ? json_decode($store_ids, true) : []);

        $goods_category_names = $req_data['goods_category_names'];
        $goods_category_names = is_array($goods_category_names) ? $goods_category_names : (is_array(json_decode($goods_category_names, true)) ? json_decode($goods_category_names, true) : []);

        foreach ($store_ids as $store_id) {
            foreach ($goods_category_names as $goods_category_name) {
                $one = GoodsCategory::where([
                    'store_id' => $store_id,
                    'category_name' => $goods_category_name
                ])->first();
                if ($one) {
                    $one = $one->toArray();
                    $goods_category_id = $one['id'];

                    $changegoods_data = [
                        'cp_id' => $cp_id,
                        'store_id' => $store_id,
                        'goods_category_id' => $goods_category_id,
                        'type' => $req_data['type'],
                        'value' => !empty($req_data['value']) ? $req_data['value'] : '',
                        'rate' => !empty($req_data['rate']) ? $req_data['rate'] : '',
                        'multiplier' => !empty($req_data['multiplier']) ? $req_data['multiplier'] : 0,
                        'start_time' => $req_data['start_time'],
                        'end_time' => $req_data['end_time'],
                        'min_discount' => !empty($req_data['min_discount']) ? $req_data['min_discount'] : 0,
                        'create_account_id' => $accountinfo['id'],
                        'create_account_real_name' => $accountinfo['real_name'],
                        'create_at' => time(),
                    ];
                    Score::create($changegoods_data);
                }
            }
        }
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $list = Score::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 详情
     */
    public function getone(Request $request)
    {
        $one = Score::where('id', $request['id'])->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '000', 'msg' => '获取失败']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'store_id', 'goods_category_id', 'type', 'value', 'rate', 'multiplier', 'start_time', 'end_time', 'min_discount']);
        $Validate = new ValidateScore('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $data = $req_data;
        unset($data['id']);
        $data['rate']                     = !empty($req_data['rate']) ? $req_data['rate'] : '';
        $data['multiplier']               = !empty($req_data['multiplier']) ? $req_data['multiplier'] : 0;
        $data['min_discount']             = !empty($req_data['min_discount']) ? $req_data['min_discount'] : 0;
        $data['update_account_id']        = $accountinfo['id'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        $data['update_at']                = time();
        Score::where('id', $req_data['id'])->update($data);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidateScore('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $req_data['is_del']                   = 2;
        $req_data['update_account_id']        = $accountinfo['id'];
        $req_data['update_account_real_name'] = $accountinfo['real_name'];
        $req_data['update_at']                = time();

        Score::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
